/*
 * accountstore.h - accounts storage class
 * Copyright (C) 2009  Sergei Stolyarov
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Kajim::ChatInputText widget implements basic functions required
 * by chat text entry widget: history navigation, autocompletion etc
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#ifndef ACCOUNTSHOLDER_H
#define ACCOUNTSHOLDER_H
#include <QObject>
#include <QHash>
#include <QList>
#include <QStringList>
#include <qmetatype.h>

#include "handlers.h"

namespace Kajim {

struct Account;

class AccountStore : public QObject
{
    Q_OBJECT
public:
    static AccountStore * instance();
    ~AccountStore();

    /**
     * @brief Get list of all accounts names.
     * @return a QStringList object with all names.
     */
    QStringList accountNames();

    /**
     * @brief Get list of all accounts.
     * @return list of all accounts' handlers.
     */
    QList<int> accountsList();

    /**
     * @brief Get account by its name.
     * @param name
     * @return
     */
    Account * accountByName(const QString & name);

    /**
     * @brief Get account by the handler.
     * @param handler
     * @return
     */
    Account * account(const int handler);

    /**
     * @brief Add account to the store and return handler of it.
     * @param account
     * @return Handler of just added account.
     */
    int addAccount(Account * account);

    /**
     * @brief Update account in the store using pointer to account.
     * @param account
     * @param newAccount
     * @return true if account was updated, false in all other cases including situation
     *      when account wasn't found in the store.
     */
    bool updateAccount(Account * account, Account * newAccount);

    /**
     * @brief Update account using store account handler.
     * @param h Handler of the account.
     * @param newAccount
     * @return true if account was updated, false in all other cases
     */
    bool updateAccount(int h, Account * newAccount);

    /**
     * @brief Remove account from the store.
     * @param account
     */
    void removeAccount(Account * account);

    /**
     * @brief Remove account from the store using account handler.
     * @param account
     */
    void removeAccount(int h);


    /**
     * @brief Synchronize accounts in the store with config.
     */
    void sync();

signals:
    void AccountAdded(int h);
    void AccountUpdated(int h);
    void AccountRemoved(int h);

private:
    static AccountStore * inst;
    struct Private;
    Private * d;

    AccountStore(QObject *parent = 0);
};

//void initAccounts();
//QStringList getAccountsList();
//Account * getAccount(const QString accountName);
//void removeAccount(Account*);
//void sync();

}

Q_DECLARE_METATYPE(Kajim::Account*)
#endif
